﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace sqlsearcher.Services
{
    public static class StaticExtends
    {
        public static string Format(this string sourceString, Object paramValues)
        {
            foreach (System.Reflection.PropertyInfo property in paramValues.GetType().GetProperties())
            {
                object parseValue = property.GetValue(paramValues, null);
                sourceString = sourceString.Replace("$" + property.Name, parseValue == null ? string.Empty : parseValue.ToString());
            }
            return sourceString;
        }


        public static List<T> ToList<T>(this DataTable dt) where T : new()
        {
            List<T> ts = new List<T>();
            Type type = typeof(T);
            string tempName = string.Empty;
            foreach (DataRow dr in dt.Rows)
            {
                T t = new T();
                PropertyInfo[] propertys = t.GetType().GetProperties();
                foreach (PropertyInfo pi in propertys)
                {
                    tempName = pi.Name;

                    if (dt.Columns.Contains(tempName))
                    {
                        if (!pi.CanWrite) continue;

                        object value = dr[tempName];

                        if (value != DBNull.Value)
                            pi.SetValue(t, value, null);
                    }
                }

                FieldInfo[] fields = t.GetType().GetFields();
                foreach (FieldInfo fi in fields)
                {
                    tempName = fi.Name;

                    if (dt.Columns.Contains(tempName))
                    {
                        object value = dr[tempName];

                        if (value != DBNull.Value)
                            fi.SetValue(t, value);
                    }
                }

                ts.Add(t);
            }
            return ts;
        }

        #region Object转short

        /// <summary>
        /// 转换为short，默认值：short.MinValue
        /// </summary>
        /// <param name="strText">需要转换的值</param>
        /// <returns></returns>
        public static short TryShort(this Object strText)
        {
            return TryShort(strText, short.MinValue);
        }

        /// <summary>
        /// 转换为short
        /// </summary>
        /// <param name="strText">需要转换的值</param>
        /// <param name="defValue">默认值</param>
        /// <returns></returns>
        public static short TryShort(this Object strText, short defValue)
        {
            short result;
            return short.TryParse(strText + "", out result) ? result : defValue;
        }

        /// <summary>
        /// 转换为short
        /// </summary>
        /// <param name="strText">需要转换的值</param>
        /// <param name="defValue">默认值</param>
        /// <returns></returns>
        public static short? TryShort(this Object strText, short? defValue)
        {
            short result;
            return short.TryParse(strText + "", out result) ? result : defValue;
        }

        #endregion

        #region Object转Int

        /// <summary>
        /// 转换为Int，默认值：int.MinValue
        /// </summary>
        /// <param name="strText">需要转换的值</param>
        /// <returns></returns>
        public static int TryInt(this Object strText)
        {
            return TryInt(strText, int.MinValue);
        }

        /// <summary>
        /// 转换为Int
        /// </summary>
        /// <param name="strText">需要转换的值</param>
        /// <param name="defValue">默认值</param>
        /// <returns></returns>
        public static int TryInt(this Object strText, int defValue)
        {
            int temp;
            return int.TryParse(strText + "", out temp) ? temp : defValue;
        }

        /// <summary>
        /// 转换为Int
        /// </summary>
        /// <param name="strText">需要转换的值</param>
        /// <param name="defValue">默认值</param>
        /// <returns></returns>
        public static int? TryInt(this Object strText, int? defValue)
        {
            int temp;
            return int.TryParse(strText + "", out temp) ? temp : defValue;
        }

        #endregion

        #region Object转Double

        /// <summary>
        /// 转换为Double，默认值：double.MinValue
        /// </summary>
        /// <param name="strText">需要转换的值</param>
        /// <returns></returns>
        public static double TryDouble(this Object strText)
        {
            return TryDouble(strText, double.MinValue);
        }

        /// <summary>
        /// 转换为Double
        /// </summary>
        /// <param name="strText">需要转换的值</param>
        /// <param name="defValue">默认值</param>
        /// <returns></returns>
        public static double TryDouble(this Object strText, double defValue)
        {
            double temp;
            return double.TryParse(strText + "", out temp) ? temp : defValue;
        }

        /// <summary>
        /// 转换为Double
        /// </summary>
        /// <param name="strText">需要转换的值</param>
        /// <param name="defValue">默认值</param>
        /// <returns></returns>
        public static double? TryDouble(this Object strText, double? defValue)
        {
            double temp;
            return double.TryParse(strText + "", out temp) ? temp : defValue;
        }

        #endregion

        #region Object转Decimal
        /// <summary>
        /// 转换为Decimal，默认值：decimal.MinValue
        /// </summary>
        /// <param name="strText">需要转换的值</param>
        /// <returns></returns>
        public static decimal TryDecimal(this Object strText)
        {
            return TryDecimal(strText, decimal.MinValue);
        }

        /// <summary>
        /// 转换为Decimal
        /// </summary>
        /// <param name="strText">需要转换的值</param>
        /// <param name="defValue">默认值</param>
        /// <returns></returns>
        public static decimal TryDecimal(this Object strText, decimal defValue)
        {
            decimal temp;
            return decimal.TryParse(strText + "", out temp) ? temp : defValue;
        }

        /// <summary>
        /// 转换为Decimal
        /// </summary>
        /// <param name="strText">需要转换的值</param>
        /// <param name="defValue">默认值</param>
        /// <returns></returns>
        public static decimal? TryDecimal(this Object strText, decimal? defValue)
        {
            decimal temp;
            return decimal.TryParse(strText + "", out temp) ? temp : defValue;
        }
        #endregion

        #region Object转Long
        /// <summary>
        /// 转换为long，默认值：long.MinValue
        /// </summary>
        /// <param name="strText">需要转换的值</param>
        /// <returns></returns>
        public static long TryLong(this Object strText)
        {
            return TryLong(strText, long.MinValue);
        }

        /// <summary>
        /// 转换为long
        /// </summary>
        /// <param name="strText">需要转换的值</param>
        /// <param name="defValue">默认值</param>
        /// <returns></returns>
        public static long TryLong(this Object strText, long defValue)
        {
            long temp;
            return long.TryParse(strText + "", out temp) ? temp : defValue;
        }

        /// <summary>
        /// 转换为long
        /// </summary>
        /// <param name="strText">需要转换的值</param>
        /// <param name="defValue">默认值</param>
        /// <returns></returns>
        public static long? TryLong(this Object strText, long? defValue)
        {
            long temp;
            return long.TryParse(strText + "", out temp) ? temp : defValue;
        }
        #endregion

        #region Object转Bool
        /// <summary>
        /// 转换为Boolean，默认值：false
        /// </summary>
        public static Boolean TryBool(this Object strText)
        {
            return TryBool(strText, false);
        }

        /// <summary>
        /// 转换为Boolean
        /// </summary>
        /// <param name="strText">需要转换的值</param>
        /// <param name="defValue">默认值</param>
        /// <returns></returns>
        public static Boolean TryBool(this Object strText, bool defValue)
        {
            if (strText.TryInt(0) == 1)
                return true;
            bool temp;
            return bool.TryParse(strText + "", out temp) ? temp : defValue;
        }

        /// <summary>
        /// 转换为Boolean
        /// </summary>
        /// <param name="strText">需要转换的值</param>
        /// <param name="defValue">默认值</param>
        /// <returns></returns>
        public static Boolean? TryBool(this Object strText, bool? defValue)
        {
            var i = strText.TryInt(null);
            if (i.HasValue)
                return i == 1;
            bool temp;
            return bool.TryParse(strText + "", out temp) ? temp : defValue;
        }
        #endregion

        #region Object转DateTime
        /// <summary>
        /// 转换为DateTime，默认值：DateTimeExtension.DBNull
        /// </summary>
        /// <param name="strText">需要转换的值</param>
        /// <returns></returns>
        public static DateTime TryDateTime(this Object strText)
        {
            return TryDateTime(strText, DateTime.Now);
        }

        /// <summary>
        /// 转换为DateTime
        /// </summary>
        /// <param name="strText">需要转换的值</param>
        /// <param name="defValue">默认值</param>
        /// <returns></returns>
        public static DateTime TryDateTime(this Object strText, DateTime defValue)
        {
            DateTime temp;
            return DateTime.TryParse(strText + "", out temp) ? temp : defValue;
        }

        /// <summary>
        /// 转换为DateTime
        /// </summary>
        /// <param name="strText">需要转换的值</param>
        /// <param name="defValue">默认值</param>
        /// <returns></returns>
        public static DateTime? TryDateTime(this Object strText, DateTime? defValue)
        {
            DateTime temp;
            return DateTime.TryParse(strText + "", out temp) ? temp : defValue;
        }
        #endregion

        #region Object转String
        /// <summary>
        /// 为NULL 和 DBNull的返回String.Empty
        /// </summary>
        /// <param name="strText">需要转换的值</param>
        /// <returns></returns>
        public static string TryString(this Object strText)
        {
            return TryString(strText, "");
        }

        /// <summary>
        /// 为NULL 和 DBNull的返回String.Empty
        /// </summary>
        /// <param name="strText">需要转换的值</param>
        /// <param name="defValue">默认值</param>
        /// <returns></returns>
        public static string TryString(this Object strText, string defValue)
        {
            return strText == null ? defValue : strText.ToString();
        }
        #endregion

        #region 十进制转十六进制
        /// <summary>
        /// 转换十六进制
        /// </summary>
        /// <param name="i"></param>
        /// <returns></returns>
        public static string ConvertX8(this int i)
        {
            return Convert.ToString(i, 16);
        }
        #endregion



    }
}
